package com.twotiger.and.activity.user;

import java.util.HashMap;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.ConstantDataUnit;
import com.twotiger.and.util.ActivitySwitcher;
import com.twotiger.and.util.RegexTool;
import com.twotiger.and.util.StringUtils;
import com.twotiger.and.util.ViewUtils;

public class FindPwdNext extends BaseActivity {

    private static final int MODIFYPSWSUCCES = 0;
    private static final int MODIFYPSWFAIL = 1;
    private EditText set_psw_et;
    private ImageView psw_isvisable;
    private Button next_btn;
    private HashMap<String, String> parm;

    private SubTitle subTitle;
    private String token;
    
    private boolean isShow = false;
    private TextView tv_backtologin;
    private ImageView iv_finish;
    private View findpwdnext_root;

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.findpwd_next_layout, null);
        if("forward".equals(getIntent().getStringExtra("isNeedAnim"))){
            ActivitySwitcher.animationInForward(view, getWindowManager());
        }
        return view;
    }

    @Override
    protected void findViewById(View view) {
        iv_finish = (ImageView)view.findViewById(R.id.iv_finish);
        tv_backtologin = (TextView) view.findViewById(R.id.tv_backtologin);
        set_psw_et = (EditText) view.findViewById(R.id.set_psw_et);
        psw_isvisable = (ImageView) view.findViewById(R.id.psw_isvisable);
        next_btn = (Button) view.findViewById(R.id.next_btn);
        findpwdnext_root = view.findViewById(R.id.findpwdnext_root);
        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.setTitle("设置登录密码");
        subTitle.hideBtns();
        subTitle.title_left_tv.setVisibility(View.VISIBLE);
        subTitle.title_left_tv.setText("取消");
        subTitle.title_left_tv.setTextColor(getResources().getColor(R.color.gray_1));
    }

    @Override
    protected void processLogic() {
        parm = getParm();
        if(getIntent().getExtras()!=null){
            token = (String) getIntent().getExtras().get("token");
        }
    }

    @Override
    protected void setListener() {
        handler = new BaseHandler(this) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case MODIFYPSWSUCCES:
                    Basebean result_pw = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result_pw.isOk()) {
                        Basebean basebean = JSON.parseObject(result_pw.data, Basebean.class);
                        showToast("修改密码成功");
//                        startNewActivity(new Intent(mContext, LoginPage.class), R.anim.push_left_in, R.anim.push_left_out, true);
                        finish();
                    } else {
                        showToast(result_pw.codeDesc);
                    }
                    break;
                case MODIFYPSWFAIL:
                    showToast("修改失败");
                    break;
                default:
                    break;
                }
                super.handleMessage(msg);
            }
        };
        iv_finish.setOnClickListener(this);
        tv_backtologin.setOnClickListener(this);
        next_btn.setOnClickListener(this);
        psw_isvisable.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if (isShow) {
                    set_psw_et.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    psw_isvisable.setBackgroundResource(R.drawable.password_plan_on);
                    isShow = false;
                } else {
                    set_psw_et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    psw_isvisable.setBackgroundResource(R.drawable.password_plan_off);
                    isShow = true;
                }
            }
        });
        set_psw_et.addTextChangedListener(new TextWatcher() {
            
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString()) || s.toString().length() < 8 ) {
                    next_btn.setEnabled(false);
                } else {
                    next_btn.setEnabled(true);
                }
            }
        });
    }

    @Override
    protected void onBackPress() {
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.iv_finish:
            finish();
            overridePendingTransition(R.anim.silent_anim, R.anim.slide_out_to_bottom);
            break;
        case R.id.tv_backtologin: 
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
//            Intent intent = new Intent(this, LoginPage.class);
//            intent.putExtra("FROM", R.id.tv_backtologin);
//            startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, true);

            final Intent intent1 = new Intent(getApplicationContext(), LoginPage.class);
            intent1.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            ActivitySwitcher.animationOutBack(findpwdnext_root, getWindowManager(), new ActivitySwitcher.AnimationFinishedListener() {
                @Override
                public void onAnimationFinished() {
                    startNewActivity(intent1.putExtra("isNeedAnim", "back"), 0, 0, true);
                }
            });
            break;
        case R.id.next_btn:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            String pswstr = set_psw_et.getText().toString().trim();
            ConstantDataUnit zz_mm = getConstantDataByKey(Constants.zz_mm);
            if(zz_mm != null){
                if (!RegexTool.match(zz_mm.getVal(), pswstr)) {
                    showToast("密码仅支持8-32位数字,字母和常量符号");
                    return;
                }
            }else {
                if (!RegexTool.match(RegexTool.REG_PASSWORD, pswstr)) {
                    showToast("密码仅支持8-32位数字,字母和常量符号");
                    return;
                }
            }
            modifypsw(pswstr);
            break;

        default:
            break;
        }
    }

    private void modifypsw(String pswstr) {
        parm.clear();
        parm.put("version", "2");
        parm.put("token", token);
        parm.put("password", pswstr);
        postDate(parm, Constants.FORGETPWD, handler, MODIFYPSWSUCCES, MODIFYPSWFAIL, true, false, false);
    }

}
